package com.ziyougou.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ziyougou.product.entity.AttrAttrgroupRelationEntity;
import com.ziyougou.product.entity.AttrGroupEntity;
import com.ziyougou.product.entity.CategoryEntity;
import com.ziyougou.product.service.AttrAttrgroupRelationService;
import com.ziyougou.product.service.AttrGroupService;
import com.ziyougou.product.service.CategoryService;
import com.ziyougou.product.vo.AttrResponseVO;
import com.ziyougou.product.vo.AttrVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.Query;

import com.ziyougou.product.dao.AttrDao;
import com.ziyougou.product.entity.AttrEntity;
import com.ziyougou.product.service.AttrService;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrGroupService attrGroupService;

    @Override
    public List<AttrEntity> getAttrListByCatelogId(Long catelogId) {
        LambdaQueryWrapper<AttrEntity> attrEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        attrEntityLambdaQueryWrapper.eq(AttrEntity::getCatelogId,catelogId);
        List<AttrEntity> list = this.baseMapper.selectList(attrEntityLambdaQueryWrapper);
        return list;
    }


    @Override
    public void updateAttrById(AttrVO attr) {
        //1.更新AttrEntity表
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        this.baseMapper.updateById(attrEntity);
        //2.更新attr_attrgroup_relation表
        // 如果新增时没有添加分组关系，则中间中就不会有数据，就无法更新
        // 如果该属性已经存在属性分组，则做更新，如果不存在则做新增
        //2.1 根据属性id，查询分组的条数
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = wrapper.eq(AttrAttrgroupRelationEntity::getAttrId, attr.getAttrId());
        AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationService.getOne(queryWrapper);
        //2.2 判断
        if(relationEntity==null){
            //做新增
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attr.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        }else{
            //做修改
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationService.updateById(relationEntity);
        }


    }

    @Override
    public AttrResponseVO getAttrById(Long attrId) {
        AttrResponseVO responseVO = new AttrResponseVO();
        //1.获取attr表中的数据
        AttrEntity attr = this.getById(attrId);
        BeanUtils.copyProperties(attr,responseVO);
        //2.查看分组的id, attr_attrgroup_relation
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrAttrgroupRelationEntity::getAttrId,attrId);
        AttrAttrgroupRelationEntity attrgroupRelationEntity = attrAttrgroupRelationService.getOne(wrapper);
        if(attrgroupRelationEntity!=null){
            responseVO.setAttrGroupId(attrgroupRelationEntity.getAttrGroupId());
        }
        //2.1 查询分组名称
       if(attrgroupRelationEntity!=null){
           AttrGroupEntity groupEntity = attrGroupService.getById(attrgroupRelationEntity.getAttrGroupId());
           responseVO.setGroupName(groupEntity.getAttrGroupName());
       }
        //3.分类的全路径
        Long[] path = categoryService.findParentPathByCategoryId(attr.getCatelogId());
        responseVO.setCatelogPath(path);

        return responseVO;
    }

    @Override
    public void saveAttr(AttrVO attr) {
        //1.attr表中的数据
        AttrEntity attr1 = new AttrEntity();
        BeanUtils.copyProperties(attr,attr1);
        this.baseMapper.insert(attr1);
        //2.attr_attrgroup_relation表
        AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
        entity.setAttrId(attr1.getAttrId());
        entity.setAttrGroupId(attr.getAttrGroupId());
        attrAttrgroupRelationService.save(entity);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catId,String type) {
        //1.获取模糊查条件
        String key =(String) params.get("key");
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<AttrEntity>();
        wrapper.eq(AttrEntity::getAttrType,"base".equals(type)?1:0);// 1 -基本属性  0-销售属性
        if(!StringUtils.isEmpty(key)){
            wrapper.like(AttrEntity::getAttrName,key);
        }
        if(catId.longValue()>0){
            wrapper.eq(AttrEntity::getCatelogId,catId);
        }
        //2.分页条件
        Long pageIndex= Long.parseLong((String)params.get("page"));
        Long limit= Long.parseLong((String)params.get("limit"));
        Page<AttrEntity> p1 = new Page<>(pageIndex,limit);
        //3.执行查询
        Page<AttrEntity> page = baseMapper.selectPage(p1, wrapper);

        //4.查询分组和分类的数据封装到AttrResponseVO类中
        List<AttrEntity> records = page.getRecords();
        List<AttrResponseVO> responseVOList=new ArrayList<>();
        for (AttrEntity record : records) {
            //4.1 查询分类(主键查询)
            CategoryEntity category = categoryService.getById(record.getCatelogId());
            //4.2 获取组名称-查询attr_attrgroup_relation
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity= attrAttrgroupRelationService.selectAttrGroupIdByAttrId(record.getAttrId());
            //获取组名称
            AttrResponseVO attrResponseVO = new AttrResponseVO();
            AttrGroupEntity attrGroup =null;
           if(attrAttrgroupRelationEntity!=null){
               attrGroup = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
               attrResponseVO.setGroupName(attrGroup.getAttrGroupName());
           }
            attrResponseVO.setCategroyName(category.getName());
            //4.3 属性表中的其他字段
            BeanUtils.copyProperties(record,attrResponseVO);
            responseVOList.add(attrResponseVO);
        }
        PageUtils utils = new PageUtils(page);
        utils.setList(responseVOList);

        return utils;
    }

}